﻿using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime;
using System.Text;
using System.Threading.Tasks;
using Verse.AI;
using Verse;
using static UnityEngine.GraphicsBuffer;

namespace Meow
{
    public class Job_GoAndMountOn : JobDriver
    {
        public override bool TryMakePreToilReservations(bool errorOnFailed)
        {
            return true;
        }
        public Pawn Mount { get { return job.targetA.Thing as Pawn; } }

        static SimpleCurve bodySizeFactor = new SimpleCurve
        {
            { new CurvePoint(2f, 200f) },
            { new CurvePoint(4f, 300f) },
        };

        protected override IEnumerable<Toil> MakeNewToils()
        {
            this.FailOnDespawnedNullOrForbidden(TargetIndex.A);
            this.FailOnDowned(TargetIndex.A);

            yield return LetMountParticipate();
            yield return Toils_Goto.GotoThing(TargetIndex.A, PathEndMode.Touch);
            if (pawn.interactions != null) yield return Toils_Interpersonal.WaitToBeAbleToInteract(pawn);
            yield return TryMountOn();
        }
        Toil LetMountParticipate()
        {
            return new Toil()
            {
                defaultCompleteMode = ToilCompleteMode.Never,
                initAction = delegate
                {
                    Mount.jobs.StopAll();
                    Mount.pather.StopDead();
                    //Mount.jobs.TryTakeOrderedJob(new Job(PawnMountDefs.Job_CarryingMounter, pawn) { count = 1 });
                    ReadyForNextToil();
                }
            };
        }
        Toil TryMountOn()
        {
            var mount = Mount;
            int mountComplexity = (int)(bodySizeFactor.Evaluate(mount.BodySize));
            return new Toil()
            {
                defaultCompleteMode = ToilCompleteMode.Delay,
                defaultDuration = 1,
                endConditions = new List<System.Func<JobCondition>>()
                {
                    delegate
                    {
                        //if (mount?.CurJobDef != PawnMountDefs.Job_CarryingMounter)
                        //{
                        //    return JobCondition.Incompletable;
                        //}
                        return JobCondition.Ongoing;
                    }
                },
                initAction = delegate
                {
                    Pawn actor = GetActor();
                    if (actor.interactions != null)
                    {
                        actor.interactions.TryInteractWith(mount, InteractionDefOf.Chitchat);
                    }

                },
                finishActions = new List<System.Action>() { delegate
                {
                    var carriercomp = mount.GetComp<Comp_ThingCarrier>();
                    //Final checks
                    if (!pawn.CanReserve(mount) || //Can reserve? ca
                    carriercomp == null ||
                    mount.Faction != pawn.Faction) //Switched factions mid-step? (quests)
                    {
                        return;
                    }
                    carriercomp.Carry(pawn);

                    //pawn.jobs.StopAll();
                    //pawn.jobs.EndCurrentJob(JobCondition.InterruptForced, false, false); //The StopAll above will trigger the WaitForRider job. This will stop it.
                    //pawn.pather.StopDead();

                }}
            };
        }
    }
}
